package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.example.fw.DateHelper;
import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3){
			System.out.println("Please specify parameters <amount of test data> <file> <format>");
			return;
		}
	    int amount = Integer.parseInt(args[0]);
	    File file = new File(args[1]);
	    String format = args[2];
	    
		if (file.exists()){
			System.out.println("File exists, please remove it: " + file);
			return;
		}
		
	    List<ContactData> contacts = generateRandomContact(amount);
	    if ("csv".equals(format)){
	    	saveContactToScvFile(contacts, file);
	    } else if ("xml".equals(format)) {
	    	saveContactToXmlFile(contacts, file);
	    } else
	    System.out.println("Unkown format: " + format);
	       return;
	    
		}

		private static void saveContactToXmlFile(List<ContactData> contacts, File file) throws IOException {
			XStream xstream = new XStream();
			xstream.alias("contact", GroupData.class);
			String xml = xstream.toXML(contacts);
			FileWriter writer = new FileWriter(file);
			writer.write(xml);
			writer.close();
		}

		public static List<ContactData> loadContactFromXmlFile(File file) {
			XStream xstream = new XStream();
			xstream.alias("contact", GroupData.class);
	        return (List<ContactData>) xstream.fromXML(file);
			}
		
		public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
			List<ContactData> list = new ArrayList<ContactData>();
			FileReader reader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(reader);
			String line = bufferedReader.readLine();
			while (line != null) {
				String[] part = line.split(",");
				ContactData contact = new ContactData()
			    .withFirstName(part[0])
			    .withLastName(part[1])
			    .withAddress(part[2])
			    .withTelephoneHome(part[3])
			    .withTelephoneMobil(part[4])
			    .withTelephoneWork(part[5])
			    .withEmail1(part[6])
			    .withEmail2(part[7])
			    .withBDay(part[8])
			    .withBMonth(part[9])
			    .withBYear(part[10])
			    .withAddress2(part[11])
			    .withHome2(part[12]);
				list.add(contact);
				line = bufferedReader.readLine();
			}
			bufferedReader.close();
			return list;
			}

		private static void saveContactToScvFile(List<ContactData> contacts, File file) throws IOException {
			FileWriter writer = new FileWriter(file);
			for (ContactData contact : contacts){
				writer.write(contact.getFirstname() 
						+ "," + contact.getLastname() 
						+ "," + contact.getAddress()
						+ "," + contact.getTelephonehome()
						+ "," + contact.getTelephonemobil()
						+ "," + contact.getTelephonework()
						+ "," + contact.getEmail1()
						+ "," + contact.getEmail2()
						+ "," + contact.getBday()
						+ "," + contact.getBmonth()
						+ "," + contact.getByear()
						+ "," + contact.getAddress2()
						+ "," + contact.getHome2()
						+ ",|\n");
			}
			writer.close();
			
		}

		public static List<ContactData> generateRandomContact(int amount) {
			List<ContactData> list = new ArrayList<ContactData>();
			for (int i = 0; i < 5; i++){
				String rndDate = DateHelper.randomDOB();
				ContactData contact = new ContactData()
			    .withFirstName(generateRandomString())
			    .withLastName(generateRandomString())
			    .withAddress(generateRandomString())
			    .withTelephoneHome(generateRandomString())
			    .withTelephoneMobil(generateRandomString())
			    .withTelephoneWork(generateRandomString())
			    .withEmail1(generateRandomString())
			    .withEmail2(generateRandomString())
			    .withBDay(DateHelper.generateRandomDay(rndDate))
			    .withBMonth(DateHelper.generateRandomMonth(rndDate))
			    .withBYear(DateHelper.generateRandomYear(rndDate))
			    .withAddress2(generateRandomString())
			    .withHome2(generateRandomString());
				list.add(contact);
			}
			return list;
		}
		
		public static String generateRandomString(){
			Random rnd = new Random();
			if (rnd.nextInt(3) == 0){
				return "";				
			}
			else{
				return "test" + rnd.nextInt();
			}
		}		

}
